package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.GetCurrentSessionListBean;
import com.ymatou.iapi.sellercs.parameter.GetCurrentSessionListResp;
import com.ymatou.iapi.sellercs.parameter.SellerSendMessageBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.GetCurrentSessionListCall;
import com.ymatou.iapi.sellercs.service.SellerSendMessageCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
/**
*获取当前会话列表
*TODO 这里的count不正确 但是和以前一致所以不校验
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCurrentSessionList{
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static GetCurrentSessionListBean getcurrentsessionlistBean;
	private static GetCurrentSessionListCall getcurrentsessionlistCall;
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static ImDefaultWapper ImDefaultWapper;
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取当前会话列表");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		getcurrentsessionlistBean=new GetCurrentSessionListBean();
		getcurrentsessionlistCall=new GetCurrentSessionListCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		ImDefaultWapper=new ImDefaultWapper();
		 SellerCSIMCallService.OffLine(sellerId, null);
			SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcurrentsessionlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCurrentSessionList_001() {
		Logger.start(true,"获取当前会话列表成功MessageImportantType=All");
		try {
			
//			Logger.comment("注册新用户");
//			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
//			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
//
//			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
//			String token = registerResult.get("Token").getAsString();
//			Integer userId = urrc.getUserid();
//			Logger.comment("删除旧数据");
//			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
//			//APP上线
//			SellerCSIMCallService.AppLogin(sellerId);
//			SendMessageBean sendbean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
//			SellerCSIMCallService.SendMessage(sendbean);
//			//触发分配
//			SellerCSIMCallService.Assign();
//			Thread.sleep(3000);
			
			getcurrentsessionlistBean.setCount(10);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(0l);
			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");
			
			
			List<Map> infos = ImDefaultWapper.selectTotalCount(null, sellerId);
			infos=infos.subList(0, getcurrentsessionlistBean.getCount());
			List<Map> fliteinfos= new ArrayList<>();
			Logger.debug("原始数据:"+infos);
			//数据少了 MessageType 1 是私信 其他不是要过滤
			//20124241专属客服要去掉
			infos.forEach(info->{
				if(info.get("MessageType").toString().equals("1")){
					if(info.get("OtherUserId").equals(SellerCSIMCallService.CsimId)){
						Logger.debug("去除数据:"+info);
//						nothing
					}else{
						fliteinfos.add(info);
					}
				}else{
					Logger.debug("去除数据:"+info);
				}
			});
			Logger.comment("fliteinfos:"+fliteinfos);
			List<Object> otheruidlist = fliteinfos.stream().map(m->{return Integer.valueOf(m.get("OtherUserId").toString());}).collect(Collectors.toList());
			Logger.verifyEquals(fliteinfos.size(), getcurrentsessionlistCall.getResult().size(), "验证count");
			Logger.comment("Result:"+FormatUtil.beanToGSONString(getcurrentsessionlistCall.getResult()));
			getcurrentsessionlistCall.getResult().forEach(result->{
				Integer returnuid = result.getToUserId();
				if(!otheruidlist.contains(returnuid)){
					Logger.comment("###msg not found:"+FormatUtil.beanToGSONString(result));
				}
			});
			//验证第一个数据
//			GetCurrentSessionListResp r1 = getcurrentsessionlistCall.getResult().get(0);
//			Logger.verifyEquals(false,r1.getIsImportant(),"验证是否重要");
//			Logger.verifyEquals(userId, r1.getToUserId(), "数据库验证ToUserId");	
//		    Logger.verifyEquals(regbean.getLoginId(), r1.getToUserName(), "数据库验证UserName");
//		    Logger.verifyEquals(JUserCallService.defaultImg, r1.getToUserLogo(), "数据库验证getToUserLogo");
//		    Logger.verifyEquals(false, r1.getTranslate() ,"数据库验证Translate");
//		    Logger.verifyEquals(1,r1.getUnReadMessageCount(),"数据库验证UnReadMessageCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetCurrentSessionList_002() {
		Logger.start(true,"不输入csid失败");
		try {
			getcurrentsessionlistBean.setCount(20);
//			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(new Date().getTime());
			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("500", getcurrentsessionlistCall.getString("Status"), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetCurrentSessionList_003() {
		//TODO 没有mcsid可以搜索成功？
		Logger.start(true,"不输入MasterCSId成功");
		try {
			getcurrentsessionlistBean.setCount(20);
			getcurrentsessionlistBean.setCSId(sellerId);
//			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(new Date().getTime());
			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetCurrentSessionList_004() {
		Logger.start(true,"不输入Count失败");
		try {
//			getcurrentsessionlistBean.setCount(20);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(new Date().getTime());
			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");
			Logger.verifyEquals(0, getcurrentsessionlistCall.getResult().size(), "验证没有返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetCurrentSessionList_005() {
		Logger.start(true,"不输入LongPostTime成功,查询全部");
		try {
			getcurrentsessionlistBean.setCount(20);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
//			getcurrentsessionlistBean.setLongPostTime(new Date().getTime());
			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetCurrentSessionList_006() {
		Logger.start(true,"不输入MessageImportantType默认查询全部");
		try {
			getcurrentsessionlistBean.setCount(20);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(new Date().getTime());
//			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");

			List<Map> infos = ImDefaultWapper.selectTotalCount(null, sellerId);
			infos=infos.subList(0, getcurrentsessionlistBean.getCount()+1);
			List<Map> fliteinfos= new ArrayList<>();
			Logger.debug(infos);
			//数据少了 MessageType 1 是私信 其他不是要过滤
			infos.forEach(info->{
				if(info.get("MessageType").toString().equals("1")){
					if(info.get("OtherUserId").equals(SellerCSIMCallService.CsimId)){
//						nothing
					}else{
						fliteinfos.add(info);
					}
				}
			});
			Logger.debug(fliteinfos);
			
			//Logger.verifyEquals(fliteinfos.size(), getcurrentsessionlistCall.getResult().size(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCurrentSessionList_007() {
		Logger.start(true,"获取多个当前会话列表成功");
		try {
			getcurrentsessionlistBean.setCount(50);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(0l);
			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");

			List<Map> infos = ImDefaultWapper.selectTotalCount(null, sellerId);
			infos=infos.subList(0, getcurrentsessionlistBean.getCount()+1);
			List<Map> fliteinfos= new ArrayList<>();
//			Logger.debug(infos);
			//数据少了 MessageType 1 是私信 其他不是要过滤
			infos.forEach(info->{
				if(info.get("MessageType").toString().equals("1")){
					if(info.get("OtherUserId").equals(SellerCSIMCallService.CsimId)){
//						nothing
					}else{
						fliteinfos.add(info);
					}
				}
			});
			Logger.debug(fliteinfos);
			
			//Logger.verifyEquals(fliteinfos.size(), getcurrentsessionlistCall.getResult().size(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCurrentSessionList_008() {
		Logger.start(true,"获取当前会话列表成功MessageImportantType=Yes");
		try {
			getcurrentsessionlistBean.setCount(20);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(new Date().getTime());
			getcurrentsessionlistBean.setMessageImportantType("Yes");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");
			getcurrentsessionlistCall.getResult().forEach(r->{
				Logger.verifyEquals(true, r.getIsImportant(), "MessageImportantType=Yes");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCurrentSessionList_009() {
		Logger.start(true,"获取当前会话列表成功MessageImportantType=No");
		try {
			getcurrentsessionlistBean.setCount(20);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(new Date().getTime());
			getcurrentsessionlistBean.setMessageImportantType("No");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");
			//No=ALL
//			getcurrentsessionlistCall.getResult().forEach(r->{
//				Logger.verifyEquals(false, r.getIsImportant(), "MessageImportantType=No");
//			});

			List<Map> infos = ImDefaultWapper.selectTotalCount(null, sellerId);
			infos=infos.subList(0, getcurrentsessionlistBean.getCount()+1);
			List<Map> fliteinfos= new ArrayList<>();
			Logger.debug(infos);
			//数据少了 MessageType 1 是私信 其他不是要过滤
			infos.forEach(info->{
				if(info.get("MessageType").toString().equals("1")){
					if(info.get("OtherUserId").equals(SellerCSIMCallService.CsimId)){
//						nothing
					}else{
						fliteinfos.add(info);
					}
				}
			});
			Logger.debug(fliteinfos);
			
			//Logger.verifyEquals(fliteinfos.size(), getcurrentsessionlistCall.getResult().size(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//全部消息过多每晚自动化跳过 执行过慢
	//同意一秒在分页地方断开会掉记录
	public void Tc_GetCurrentSessionList_010() {
		Logger.start(true,"获取全部会话列表成功");
		try {
			getcurrentsessionlistBean.setCount(50);
			getcurrentsessionlistBean.setCSId(sellerId);
			getcurrentsessionlistBean.setMasterCSId(sellerId);
			getcurrentsessionlistBean.setLongPostTime(0l);
			getcurrentsessionlistBean.setMessageImportantType("All");
			getcurrentsessionlistCall.setData(getcurrentsessionlistBean);
			getcurrentsessionlistCall.callService();			
			Logger.verifyEquals("200", getcurrentsessionlistCall.getString("Status"), "验证Status");
			ArrayList<GetCurrentSessionListResp> results = getcurrentsessionlistCall.getResult();
			int allsize=0;
			while(results.size()>0){
				allsize+=results.size();
				getcurrentsessionlistBean.setLongPostTime(results.get(results.size()-1).getLastMessageTime().getTime());;
				getcurrentsessionlistCall.callService();		
				results = getcurrentsessionlistCall.getResult();
			}
			
			List<Map> infos = ImDefaultWapper.selectTotalCount(null, sellerId);
//			infos=infos.subList(0, getcurrentsessionlistBean.getCount()+1);
			List<Map> fliteinfos= new ArrayList<>();
//			Logger.debug(infos);
			//数据少了 MessageType 1 是私信 其他不是要过滤
			infos.forEach(info->{
				if(info.get("MessageType").toString().equals("1")){
					if(info.get("OtherUserId").equals(SellerCSIMCallService.CsimId)){
//						nothing
					}else{
						fliteinfos.add(info);
					}
				}
			});
			Logger.debug(fliteinfos);
			
			Logger.verifyEquals(fliteinfos.size(), allsize, "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}